package com.test.clientsocket;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.Buffer;

import com.zack.util.DataTransfer;

public class SocketClient {
	public static void main(String[] args) {
		String name = "";
		try {
			System.out.println("请输入昵称：");
			 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in,"UTF-8"));
			name = reader.readLine();
			
			Socket client = new Socket("117.48.199.119",666);
		    System.out.println("创建成功,可以开始聊天!");
		    //开启线程监听服务器消息
		    Thread t = new Thread(new MessageListener(client,name));
		    t.start();
		    ObjectInputStream in = null;
		    ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream())); 
		    while(true){
		    	String sendMsg = reader.readLine();
		    	if(sendMsg.equals("end")){
		    		break;
		    	}
		    	DataTransfer dt = new DataTransfer();
		    	dt.setName(name);
		    	dt.setContent(sendMsg);
		    	out.writeObject(dt);
		    	//out.writeObject(null);;
		    	out.flush();
		    	System.out.println("你说:"+sendMsg);
		    	/*in = new ObjectInputStream(client.getInputStream());
		    	DataTransfer serverDT =  (DataTransfer) in.readObject();
		    	if(name.equals(serverDT.getName())){
		    		if(!serverDT.getIsSuccess()){
		    			System.out.println("消息发送失败!");
		    		}
		    		
		    	}
		    	*/
		    }
		    //in.close();
		    client.close();
		    out.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	

}
